﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GTA.Web.Pages
{
    public partial class Login : System.Web.UI.Page
    {
        #region Starting Page
        protected void Page_Load(object sender, EventArgs e)
        {
            StartPage();
        }
        private void StartPage()
        {
            if (!IsPostBack)
                txtLogin.Focus();
        }
        #endregion

        #region Events
        protected void btnOK_Click(object sender, EventArgs e)
        {
            LogUser();
        }
        #endregion

        #region Methods
        private void LogUser()
        {
            try
            {
                GTA.BusinessEntity.UserManagement.User user = new GTA.BusinessLogic.GTA().AuthenticateUser(txtLogin.Text, GTA.Cryptography.Util.DESCryptographer.Encrypt(txtPassword.Text));
                if (user != null)
                {
                    Session["LoggedUser"] = user;
                    Response.Redirect("~/Default.aspx");
                }
                else
                {
                    lblErrorMessage.Text = "Usuário ou senha inválidos!";
                    lblErrorMessage.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lblErrorMessage.Text = "Ocorreu um erro durante a autenticação.<br/>Detalhes:&nbsp;" + GTA.Common.Util.GetFullException(ex);
                lblErrorMessage.Visible = true;
            }
        }
        #endregion
    }
}
